package cn.lijida.weiwei.favor.modules.member.service.impl;

import java.util.Calendar;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.digest.MD5;
import cn.lijida.weiwei.favor.modules.member.entity.FavorMember;
import cn.lijida.weiwei.favor.modules.member.mapper.FavorMemberMapper;
import cn.lijida.weiwei.favor.modules.member.service.IFavorMemberService;
import cn.lijida.weiwei.favor.modules.sys.bo.LoginBO;
import cn.lijida.weiwei.favor.modules.sys.entity.FavorToken;
import cn.lijida.weiwei.favor.modules.sys.service.IFavorTokenService;
import cn.lijida.weiwei.favor.modules.sys.vo.LoginVO;

/**
 * <p>
 * 成员 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-12-28
 */
@Service
public class FavorMemberServiceImpl extends ServiceImpl<FavorMemberMapper, FavorMember> implements IFavorMemberService {

	@Autowired
	private IFavorTokenService tokenService;
	
	@Value("${favor.safe.token-expira:12}")
	private Integer expiraHour;
	
	@Override
	public void register(FavorMember member) throws Exception {
		
		//密码加密
		MD5 md5 = new MD5();
		String salt = UUID.fastUUID().toString();
		md5.setSalt(salt.getBytes("UTF-8"));
		String md5Pwd = md5.digestHex(member.getMemberPassword());
		member.setMemberPassword(md5Pwd);
		member.setMemberSalt(salt);
		//保存成员
		boolean status = this.save(member);
		if(!status){
			throw new Exception("创建成员失败");
		}
	}

	@Override
	public LoginVO login(LoginBO login) throws Exception {
		
		QueryWrapper<FavorMember> queryWrapper = new QueryWrapper<>();
		queryWrapper
			.eq("member_phone", login.getUserName())
			.last("LIMIT 1");
		FavorMember member = this.getOne(queryWrapper);
		if(member == null){
			throw new Exception("用户不存在");
		}
		if(Boolean.TRUE.equals(member.getMemberStatus())){
			throw new Exception("用户已被禁用");
		}
		
		//加密密码
		MD5 md5 = new MD5();
		String salt = member.getMemberSalt();
		if(StringUtils.isNotBlank(salt)){
			md5.setSalt(salt.getBytes(CharsetUtil.UTF_8));
		}
		String md5PWD = md5.digestHex(login.getPassword());
		if(StringUtils.equalsIgnoreCase(md5PWD, member.getMemberPassword())){
			//登录成功
			FavorToken tokenEntity = new FavorToken();
			String deviceId = login.getDeviceId();
			if(StringUtils.isBlank(deviceId)){
				deviceId = UUID.fastUUID().toString();
			}
			tokenEntity.setDeviceId(deviceId);
			String token = UUID.fastUUID().toString();
			tokenEntity.setToken(token);
			tokenEntity.setMemberId(member.getMemberId());
			Calendar expira = Calendar.getInstance();
			expira.add(Calendar.HOUR_OF_DAY, expiraHour);
			tokenEntity.setExpirationDate(expira.getTime());
			tokenService.save(tokenEntity);
			
			LoginVO vo = new LoginVO();
			vo.setDeviceId(deviceId);
			vo.setToken(token);
			return vo;
		}else{
			throw new Exception("用户名或密码错误");
		}
	}

}
